
package com.cyanite.playlv2.blog;

import com.cyanite.playlv2.blog.sinasupport.OAuthConstant;
import com.cyanite.playlv2.blog.sinasupport.Weibo;
import com.cyanite.playlv2.blog.sinasupport.WeiboException;
import com.cyanite.playlv2.blog.sinasupport.http.AccessToken;
import com.cyanite.playlv2.blog.sinasupport.http.HttpClient;
import com.cyanite.playlv2.blog.sinasupport.http.ImageItem;
import com.cyanite.playlv2.blog.sinasupport.http.RequestToken;
import com.cyanite.playlv2.preferences.Preferences;


import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.widget.Toast;

import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;

public class SinaBlogManager {
    Context mCxt;

    public SinaBlogManager(Context mCxt) {
        super();
        this.mCxt = mCxt;
        System.setProperty("weibo4j.oauth.consumerKey", Weibo.CONSUMER_KEY);
        System.setProperty("weibo4j.oauth.consumerSecret", Weibo.CONSUMER_SECRET);
    }
    
    public void bind() {
        Weibo weibo = OAuthConstant.getInstance().getWeibo();
        RequestToken requestToken;
        try {
            requestToken = weibo.getOAuthRequestToken("com.cyanite.playlv2://BlogBindActivity");
            Uri uri = Uri.parse(requestToken.getAuthenticationURL() + "&from=xweibo");
            OAuthConstant.getInstance().setRequestToken(requestToken);
            mCxt.startActivity(new Intent(Intent.ACTION_VIEW, uri));
        } catch (WeiboException e) {
            e.printStackTrace();
        }
    }
    
    public Boolean handleAuthCallBack(Uri uri) {
        Boolean ret = false;
        try {
            String pin = uri.getQueryParameter("oauth_verifier");
            if (pin != null) {
                AccessToken accessToken = OAuthConstant.getInstance().getRequestToken().getAccessToken(pin);
                if (accessToken != null) {
                    Preferences.saveSinaTokenPair(mCxt, accessToken.getToken(), accessToken.getTokenSecret());
                    Toast.makeText(mCxt, "绑定新浪微博成功", Toast.LENGTH_LONG).show();
                    ret = true;
                }
            }
        } catch (WeiboException e) {
            e.printStackTrace();
        }
        return ret;
    }
    
    
    public void sendToSinaBlog(String blogContent) {
        if(Preferences.readSinaToken(mCxt).equals("")) {
            Toast.makeText(mCxt, "requiring token missed, please bind sina blog first!", Toast.LENGTH_LONG).show();
            return;
        }
        
        Weibo weibo = OAuthConstant.getInstance().getWeibo();
        weibo.setToken(Preferences.readSinaToken(mCxt), Preferences.readSinaTokenSecret(mCxt));
        try {
            String sendStr = java.net.URLEncoder.encode(blogContent, "UTF-8");
            weibo.updateStatus(sendStr);
        } catch (WeiboException e) {
            e.printStackTrace();
            Toast.makeText(mCxt, HttpClient.getCause(e.getStatusCode()), Toast.LENGTH_LONG).show();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    public void sendNewBlogWithPhoto(String blogContent, String picturePath) {
        if(Preferences.readSinaToken(mCxt).length()==0) {
            Toast.makeText(mCxt, "requiring token missed, please bind sina blog first!", Toast.LENGTH_LONG).show();
            return;
        }
        
        Weibo weibo = OAuthConstant.getInstance().getWeibo();
        weibo.setToken(Preferences.readSinaToken(mCxt), Preferences.readSinaTokenSecret(mCxt));
        try {
            String sendStr = java.net.URLEncoder.encode(blogContent, "UTF-8");
            
            BufferedInputStream bufferedInputStream = new BufferedInputStream(new FileInputStream(picturePath));
            int len = bufferedInputStream.available();
            byte[] bytes = new byte[len];
            if (bufferedInputStream.read(bytes) != len) 
                throw new IOException("failed read file");
            bufferedInputStream.close();
            ImageItem pic = new ImageItem("pic", bytes);
            weibo.uploadStatus(sendStr, pic);
        } catch (WeiboException e) {
            e.printStackTrace();
            Toast.makeText(mCxt, HttpClient.getCause(e.getStatusCode()), Toast.LENGTH_LONG).show();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
